/* Does quantile spacing perform better than even spacing when a DGP is more or
less "equally distributed" as measured by the Gini coefficient?
*/

clear
set more off
set scheme  s1mono 

cd "${main}"


cap log close
log using "${logs}/es_vs_qs_gini.log", replace	

use "${dat}/survey_merged_rd_estimates_micro.dta", clear

cap drop _merge

* Generate indicator for even spacing or quantile spacing
gen spacing = substr(filename, -2, 2)
gen qs = spacing == "qs"
replace spacing = "Even Spacing" if spacing == "es"
replace spacing = "Quantile Spacing" if spacing == "qs"

* Create dummies for being correct
cap drop disc
gen disc  = 0 if abs_disc<=0.01
replace disc = 4 if abs_disc>=0.89 & abs_disc<=0.91
replace disc = 5 if abs_disc>=1.49
replace disc = 3 if abs_disc>=0.53 & abs_disc<=0.55
replace disc = 2 if abs_disc>=0.32 & abs_disc<=0.33
replace disc = 1 if abs_disc>=0.19 & abs_disc<=0.2

label define disc_lab 0 "Zero" 1 "0.1944" 2 "0.324" 3 "0.54" 4 "0.9" 5 "1.5"
label values disc disc_lab
tabulate disc, gen(d)

gen playercorrect = y == 0 & d1 == 1 | y == 1 & d1 == 0

* Regression-based test for whether QS does significantly differently for each DGP
foreach paper in "ADGP1" "BDGP2" "CDGP3" "DDGP4" "EDGP5" "FDGP6" "GDGP7" "HDGP8" "IDGP9" "JDGP10" "KDGP11" {
	preserve
	di "`paper'"
	keep if playerdgp == "`paper'"
	reg playercorrect qs, r
	restore
}

* Collapse to DGP-specific means by spacing
collapse (mean) playercorrect, by(playerdgp spacing)

rename playerdgp paper

merge m:1 paper using "${dat}/gini_coefficients_rd.dta"

sepscatter playercorrect gini_es, separate(spacing) ///
	xtitle("Gini Coefficient (Large Bins)") ///
	ytitle("Proportion of Participants Correct") ///
	xscale(range(0 1)) xlabel(#6) ///
	yscale(range(0 1)) ylabel(#6)
graph export "${output}/es_vs_qs_big_bins_gini.pdf", as(pdf) replace

sepscatter playercorrect gini_esmv, separate(spacing) ///
	xtitle("Gini Coefficient (Small Bins)") ///
	ytitle("Proportion of Participants Correct") ///
	xscale(range(0 1)) xlabel(#6) ///
	yscale(range(0 1)) ylabel(#6)
graph export "${output}/es_vs_qs_small_bins_gini.pdf", as(pdf) replace

graph close _all

log close
